import {defineStore} from 'pinia'
import {getTagDetail, getTagList} from '@/api/index.js'
import {ElMessage} from "element-plus";

export const useTagStore = defineStore('Tag', {
    state: () => ({
        tags: {},
        loading: false,
        error: null
    }),

    actions: {
        async fetchTags() {
            this.loading = true
            this.error = null
            try {
                const res = await getTagList()
                this.tags = res.data.results || res.data
            } catch (err) {
                this.error = err
                ElMessage.error('分类获取失败:', err)
            } finally {
                this.loading = false
            }
        }
    }
})


export const useTagDetailStore = defineStore('TagDetail', {
    state: () => ({
        tagDetail: {},
        loading: false,
        error: null
    }),

    actions: {
        async fetchTagDetail(id, slug) {
            this.loading = true
            this.error = null
            try {
                const res = await getTagDetail(id, slug)
                console.log(res)
                this.tagDetail = res.data
            } catch (err) {
                this.error = err
                ElMessage.error('获取文章详情失败:', err)
            } finally {
                this.loading = false
            }
        },
    }
})